package com.getpebble.android.framework.appmessage;

import com.getpebble.android.framework.appmessage.PebbleTuple.TupleType;
import com.getpebble.android.framework.appmessage.PebbleTuple.Width;
import com.getpebble.android.framework.util.ByteUtils;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;
import org.json.JSONObject;

public class BinaryPebbleDictionary extends PebbleDictionary {

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType = new int[TupleType.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$Width = new int[Width.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$Width[Width.BYTE.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$Width[Width.SHORT.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$Width[Width.WORD.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.BYTES.ordinal()] = 1;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.STRING.ordinal()] = 2;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.INT.ordinal()] = 3;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.UINT.ordinal()] = 4;
            } catch (NoSuchFieldError e7) {
            }
        }
    }

    public BinaryPebbleDictionary(PebbleDictionary other) {
        super(other);
    }

    public ByteBuffer toByteBuffer() {
        Collection<PebbleTuple> values = this.tuples.values();
        int bufferSize = 1;
        for (PebbleTuple t : values) {
            bufferSize += getTupleByteSize(t);
        }
        ByteBuffer buf = ByteBuffer.allocate(bufferSize);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.put((byte) values.size());
        for (PebbleTuple t2 : values) {
            buf.put(serializeTuple(t2));
        }
        buf.rewind();
        return buf;
    }

    public static BinaryPebbleDictionary fromByteBuffer(ByteBuffer buff) {
        ByteBuffer b = buff.duplicate();
        b.order(ByteOrder.LITTLE_ENDIAN);
        int numKeys = b.get();
        BinaryPebbleDictionary d = new BinaryPebbleDictionary();
        for (int k = 0; k < numKeys; k++) {
            d.addTuple(deserializeTuple(b));
        }
        return d;
    }

    static int getTupleByteSize(PebbleTuple t) {
        return t.length + 7;
    }

    static PebbleTuple deserializeTuple(ByteBuffer buff) {
        ByteBuffer b = buff.duplicate();
        b.order(ByteOrder.LITTLE_ENDIAN);
        int key = b.getInt();
        TupleType type = PebbleTuple.TYPE_ORDINALS[b.get() & 255];
        int size = b.getShort() & 65535;
        PebbleTuple result = null;
        Width w;
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                Object byteArray = new byte[size];
                b.get(byteArray, 0, size);
                result = PebbleTuple.create(key, type, Width.NONE, byteArray);
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                result = PebbleTuple.create(key, type, Width.NONE, ByteUtils.getStringFromBuffer(b, size));
                break;
            case ListInfo.INDETERMINATE /*3*/:
                long i;
                if (size == 2) {
                    i = (long) b.getShort();
                    w = Width.SHORT;
                } else if (size == 4) {
                    i = (long) b.getInt();
                    w = Width.WORD;
                } else {
                    i = (long) b.get();
                    w = Width.BYTE;
                }
                result = PebbleTuple.create(key, type, w, Long.valueOf(i));
                break;
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                long u;
                if (size == 2) {
                    u = (long) (b.getShort() & 65535);
                    w = Width.SHORT;
                } else if (size == 4) {
                    u = ((long) b.getInt()) & -1;
                    w = Width.WORD;
                } else {
                    u = (long) (b.get() & 255);
                    w = Width.BYTE;
                }
                result = PebbleTuple.create(key, type, w, Long.valueOf(u));
                break;
        }
        buff.position(b.position());
        return result;
    }

    static ByteBuffer serializeTuple(PebbleTuple t) {
        ByteBuffer buf = ByteBuffer.allocate(getTupleByteSize(t));
        buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.putInt(t.key);
        buf.put(t.type.ord);
        buf.putShort((short) (t.length & 65535));
        switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[t.type.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                buf.put((byte[]) t.value);
                break;
            case SMTPMessage.RETURN_HDRS /*2*/:
                buf.put(ByteUtils.getFixedLengthString((String) t.value, t.length));
                break;
            case ListInfo.INDETERMINATE /*3*/:
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                long l = ((Long) t.value).longValue();
                switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$Width[t.width.ordinal()]) {
                    case SMTPMessage.RETURN_FULL /*1*/:
                        buf.put((byte) ((int) (255 & l)));
                        break;
                    case SMTPMessage.RETURN_HDRS /*2*/:
                        buf.putShort((short) ((int) (65535 & l)));
                        break;
                    case ListInfo.INDETERMINATE /*3*/:
                        buf.putInt((int) (-1 & l));
                        break;
                    default:
                        break;
                }
        }
        buf.rewind();
        return buf;
    }

    public JSONObject toJson() {
        try {
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonSuperObject = super.toJson();
            jsonObject.put("instance", getClass().getSimpleName());
            String str = "contents";
            if (jsonSuperObject == null) {
                jsonSuperObject = "((null))";
            }
            jsonObject.put(str, jsonSuperObject);
            return jsonObject;
        } catch (Exception e) {
            return null;
        }
    }
}
